﻿using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace XuanFe.AppLaunch.Common.Xml
{
    /// <summary>
    /// xml转换器
    /// </summary>
    public class XmlConverter
    {
        private static XmlConverter instance = new XmlConverter();

        public static XmlConverter Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// 从文件中反序列化为数据实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="file"></param>
        /// <returns></returns>
        public T DeserializeFile<T>(string file) where T : new()
        {
            return DeserializeFile<T>(file, Encoding.GetEncoding("GB2312"));
        }
        /// <summary>
        /// 从文件中反序列化为数据实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="file"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public T DeserializeFile<T>(string file, Encoding encoding) where T : new()
        {
            if (!File.Exists(file))
            {
                T config = new T();
                SerializeFile(file, config, encoding);
            }
            return DeserializeObject<T>(File.ReadAllText(file, encoding));
        }
        /// <summary>
        /// 将xml数据转化为数据实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public T DeserializeObject<T>(string value)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            XmlTextReader x = new XmlTextReader(new StringReader(value));
            T obj = (T)serializer.Deserialize(x);
            return obj;
        }
        /// <summary>
        /// 将数据实体序列化到文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="value"></param>
        public void SerializeFile(string file, object value)
        {
            SerializeFile(file, value, Encoding.GetEncoding("GB2312"));
        }
        /// <summary>
        /// 将数据实体序列化到文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        public void SerializeFile(string file, object value, Encoding encoding)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            SerializeFile(file, value, encoding, ns);
        }
        /// <summary>
        /// 将数据实体序列化到文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        /// <param name="ns"></param>
        public void SerializeFile(string file, object value, Encoding encoding, XmlSerializerNamespaces ns)
        {
            File.WriteAllText(file, SerializeObject(value, encoding, ns), encoding);
        }
        /// <summary>
        /// 将数据实体转化为xml数据
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string SerializeObject(object value)
        {
            return SerializeObject(value, Encoding.GetEncoding("GB2312"));
        }
        /// <summary>
        /// 将数据实体转化为xml数据
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public string SerializeObject(object value, Encoding encoding)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            return SerializeObject(value, encoding, ns);
        }
        /// <summary>
        /// 将数据实体转化为xml数据
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        /// <param name="ns"></param>
        /// <returns></returns>
        public string SerializeObject(object value, Encoding encoding, XmlSerializerNamespaces ns)
        {
            using (MemoryStream output = new MemoryStream())
            {
                XmlTextWriter w = new XmlTextWriter(output, encoding);
                w.Formatting = Formatting.Indented;
                XmlSerializer xs = new XmlSerializer(value.GetType(), string.Empty);
                xs.Serialize(w, value, ns);
                output.Position = 0;
                StreamReader r = new StreamReader(output, encoding);
                return r.ReadToEnd();
            }
        }
    }
}
